package com.mdd.common.utils;

import com.mdd.common.constant.GlobalConstant;
import io.swagger.annotations.ApiModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@ApiModel("随机工具类")

public class RandomUtil {

    private static Random random = new Random();

    /**
     *      随机获取 一个值
     *
     * @param list
     * @return
     * @param <T>
     */
    public static <T> T getRandomItem(List<T> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        int index = random.nextInt(list.size());
        return list.get(index);
    }
    /**
     *      随机获取 多个值
     *
     * @param list
     * @return
     * @param <T>
     */
    public static <T> List<T> getRandomItem(List<T> list, Integer number) {
        ArrayList<T> ArrayList = new ArrayList<T>();
        if (list == null || list.isEmpty()) {
            return null;
        }
        if (list.size() <= number) {
            return list;
        }
        for (int i = 0; i < number; i++) {
            int index = random.nextInt(list.size());
            ArrayList.add(list.get(index));
        }
        return ArrayList;
    }
}